package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 创建角色
func NewCreateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRoleLogic {
	return &CreateRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleRequest) (resp *types.CreateRoleResponse, err error) {
	role, err := l.svcCtx.AuthRoleRpc.CreateRole(l.ctx, &v1.CreateRoleRequest{
		RolePid:     req.RolePid,
		RoleName:    req.RoleName,
		Description: req.Description,
		Remark:      req.Remark,
		ListOrder:   req.ListOrder,
		Status:      v1.Status(req.Status),
	})

	if err != nil {
		return nil, err
	}

	return &types.CreateRoleResponse{
		Id:          role.GetId(),
		RolePid:     role.GetRolePid(),
		RoleName:    role.GetRoleName(),
		Description: role.GetDescription(),
		Remark:      role.GetRemark(),
		ListOrder:   role.GetListOrder(),
		Status:      int8(role.GetStatus()),
		CreatedAt:   role.GetCreatedAt(),
		UpdatedAt:   role.GetUpdatedAt(),
	}, err
}
